Strobogrammatic Number

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

Write a function to determine if a number is strobogrammatic. The number is represented as a string.

For example, the numbers “69”, “88”, and “818” are all strobogrammatic.

Solution:

  1. public class Solution {
  2. public boolean isStrobogrammatic(String num) {
  3. if (num == null)
  4. return false;
  5. return helper(num, 0, num.length() - 1);
  6. }
  7. boolean helper(String s, int lo, int hi) {
  8. if (lo > hi)
  9. return true;
  10. char c1 = s.charAt(lo);
  11. char c2 = s.charAt(hi);
  12. int mul = (c1 - '0') * (c2 - '0');
  13. if (mul == 1 || mul == 54 || mul == 64 || (mul == 0 && c1 == c2))
  14. return helper(s, lo + 1, hi - 1);
  15. return false;
  16. }
  17. }